package pl.swspiz;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LoginServlet extends HttpServlet {


    @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {

	try
	{	    

	User user = new User();
	
	user.setUserName(request.getParameter("un"));	
	user.setPassword(request.getParameter("pw"));	
	
	user = UserDAO.login(user);					
   
	
	
	if (user.isValid())
	{
		
		HttpSession session = request.getSession(true);	 
														 // throughout all the jsp pages
		
		session.setAttribute("currentSessionUser",user);  
														 // "currentSessionUser", so that userLogged.jsp can use it  
														 // later to display the user's first and last names
														 // Please take a look at userLogged.jsp now
			        
		response.sendRedirect(request.getContextPath()+"/listRecords"); 		 
	}
		        
	else 
		response.sendRedirect("invalidLogin.jsp"); 		 
		        
		} 
			
			
	catch (Throwable theException) 
		    
	{
		System.out.println(theException); 
	}		
	
	}	
}
